package com.vanvalt.payhui.modules.pay.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.entity.UserPlatform;
import com.vanvalt.payhui.modules.pay.service.UserPlatformService;
import com.vanvalt.payhui.modules.pay.service.UserService;

/**
 * @author Xiaoyang
 * 用户分润平台
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/platform")
public class UserPlatformController extends BaseController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private UserPlatformService userPlatformService;
	
	@ModelAttribute
	public UserPlatform get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return userPlatformService.get(id);
		}else{
			return new UserPlatform();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(@RequestParam(required=false) String uid, UserPlatform userPlatform, HttpServletRequest request, HttpServletResponse response, Model model) {

		String userId = "";
		if(StringUtils.isNotEmpty(uid)){
			userPlatform.setUserId(uid);
			userId = uid;
		}
        Page<UserPlatform> page = userPlatformService.findPage(new Page<UserPlatform>(request, response), userPlatform); 
        model.addAttribute("page", page);
        model.addAttribute("uid", userId);
        
		return "modules/pay/profitPlatformList";
	}
	
	@RequestMapping(value = "form")
	public String form(@RequestParam(required=false) String uid, UserPlatform userPlatform, Model model) {
		
		User user = null;
		if(uid != null && !"".equals(uid)){
			user = userService.get(uid);
		} else {
			if(userPlatform != null && StringUtils.isNotEmpty(userPlatform.getUserId())){
				user = userService.get(userPlatform.getUserId());
			}
		}
		
		model.addAttribute("user", user);
		model.addAttribute("userPlatform", userPlatform);
		return "modules/pay/profitPlatformForm";
	}
	
	@RequestMapping(value = "save")
	public String save(@RequestParam(required=false) String uid, UserPlatform userPlatform, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userPlatform)){
			return form(uid, userPlatform, model);
		}
		
		String userId = "";
		if(StringUtils.isNotEmpty(uid)){
			userId = uid;
		} else {
			userId = userPlatform.getUserId();
		}
		
		if(StringUtils.isEmpty(userPlatform.getId())){ // 只在添加时校验用户平台代码
			
			String platformId = userPlatform.getPlatformId();
			String platformUserCode = userPlatform.getPlatformUserCode();
			UserPlatform uPlatform = new UserPlatform();
			uPlatform.setPlatformId(platformId);
			uPlatform.setPlatformUserCode(platformUserCode);
			
			List<UserPlatform> upList = userPlatformService.findList(uPlatform);
			
			if(upList != null && upList.size() > 0){
				addMessage(redirectAttributes, "该平台用户代码已存在！");
				return "redirect:" + adminPath + "/pay/platform/form?uid="+userId;
			}
		}
		
		userPlatformService.save(userPlatform);
		
		addMessage(redirectAttributes, "保存用户分润平台信息'" + StringUtils.abbr(userPlatform.getUserName(),50) + "'成功");
		//return "redirect:" + adminPath + "/pay/platform/?repage";
		return "redirect:" + adminPath + "/pay/platform?uid="+userId;
	}
	
	@RequestMapping(value = "delete")
	public String delete(UserPlatform userPlatform, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		String userId = userPlatform.getUserId();
		
		userPlatformService.delete(userPlatform);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"用户分润平台信息成功");
		return "redirect:" + adminPath + "/pay/platform?uid="+userId;
	}
	
}
